package net.oschina.simpleplus.erpc.core;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;

import net.oschina.simpleplus.erpc.proto.ERpcRequestMessage.ERpcRequest;
import net.oschina.simpleplus.erpc.proto.ERpcRequestMessage.ERpcRequest.Builder;
import net.oschina.simpleplus.erpc.support.Protobuf;

public class ERpcRequestEntity implements Protobuf<ERpcRequest>{

	private String msgId;
	private String serviceName;
	private String methodName;
	private byte[] args;

	public String getMsgId() {
		return msgId;
	}

	public void setMsgId(String msgId) {
		this.msgId = msgId;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public byte[] getArgs() {
		return args;
	}

	public void setArgs(byte[] args) {
		this.args = args;
	}

	@Override
	public ERpcRequest build() {
		Builder builder = ERpcRequest.newBuilder().setMsgId(msgId).setServiceName(serviceName).setMethodName(methodName);
		if(args != null)
			builder.setArgs(ByteString.copyFrom(args));
		return builder.build();
	}

	@Override
	public byte[] toByteArray() {
		return build().toByteArray();
	}

	@Override
	public void parseFrom(byte[] bytes) throws InvalidProtocolBufferException {
		copyFrom(ERpcRequest.parseFrom(bytes));
	}

	@Override
	public void copyFrom(ERpcRequest t) {
		msgId = t.getMsgId();
		serviceName = t.getServiceName();
		methodName = t.getMethodName();
		args = t.getArgs().toByteArray();
	}

}
